package com.example.pets.view.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.example.pets.R;
import com.example.pets.model.entities.Post;
import com.example.pets.model.entities.dao.PostDao;
import com.example.pets.model.entities.database.AppDatabase;
import com.example.pets.view.activity.PostDetailActivity;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class PlaceFragment extends Fragment {
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private AppDatabase database;
    private PostDao postDao;
    private List<Post> postlist;

    public PlaceFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_place, container, false);

        // 初始化地图控件
        mMapView = view.findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        // 初始化数据库
        database = AppDatabase.getDatabase(getContext());
        postDao = database.postDao();



new Thread(new Runnable() {
    @Override
    public void run() {
        postlist = postDao.getAllPosts();
        for (Post post : postlist) {

            addMarker(post);
        }
    }
}).start();

        return view;
    }



    private void addMarker(Post post) {
        double latitude = post.getLatitude();
        double longitude = post.getLongitude();
        String avatarUrl = post.getAuthorurl();
        int postId = post.getPostId();

        if (latitude == 0 || longitude == 0) return;


        // 移动地图到中国中心点
        LatLng center =new LatLng(latitude, longitude);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(center));
        BitmapDescriptor bitmap;

             bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.marker_icon);



        // 构建 MarkerOption 对象
        OverlayOptions options = new MarkerOptions()
                .position(center)
                .icon(bitmap)
                .title("title")
                .draggable(false); // 是否可拖拽


        Marker marker = (Marker) mBaiduMap.addOverlay(options);

        // 保存 postId 到 Marker 的 extraInfo 中
        Bundle bundle = new Bundle();
        bundle.putInt("postId", postId);
        marker.setExtraInfo(bundle);



    }

    private void showInfoWindow(LatLng latLng, String Authorurl) {
        BitmapDescriptor bitmap;
        if(Authorurl!=null){
            File imageFile = new File(Authorurl);

            // 使用 BitmapFactory 从文件创建 Bitmap 对象
            Bitmap bitmaps = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()), 60, 60, true);
            bitmap= BitmapDescriptorFactory.fromBitmap(bitmaps);


        }else {
            bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.marker_icon);
        }
        InfoWindow infoWindow = new InfoWindow(bitmap,
                latLng, -27, new InfoWindow.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick() {
                // 点击 InfoWindow 后的操作
                mBaiduMap.hideInfoWindow();
            }
        });

        // 自定义 InfoWindow 内容
        // 这里可以创建一个自定义的 View 并设置为 InfoWindow 的内容
        // 注意：这里只是简单打印地址，你可以根据需要进一步定制 InfoWindow 的样式

        // 显示 InfoWindow
        mBaiduMap.showInfoWindow(infoWindow);
    }
    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
        setupMarkerClickListener();
    }

    private void setupMarkerClickListener() {
        mBaiduMap.setOnMarkerClickListener(marker -> {
            Bundle bundle = marker.getExtraInfo();
            if (bundle != null) {
                int postId = bundle.getInt("postId");
                Intent intent = new Intent(getActivity(), PostDetailActivity.class);
                intent.putExtra("postId", postId);
                startActivity(intent);
            }
            return true;
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
}

